import json
from channels.generic.websocket import AsyncWebsocketConsumer

RUN_CENTER_GROUP = "run_center"

class RunCenterConsumer(AsyncWebsocketConsumer):
    async def connect(self):
        await self.channel_layer.group_add(RUN_CENTER_GROUP, self.channel_name)
        await self.accept()

    async def disconnect(self, close_code):
        await self.channel_layer.group_discard(RUN_CENTER_GROUP, self.channel_name)

    async def run_center_update(self, event):
        # event: {"type": "run_center_update", "data": {...}}
        data = event.get("data", {})
        await self.send(text_data=json.dumps(data))